﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;

namespace HydrographicSurvey.HYTCPServer {
    class ClientThread {
        //connections变量表示连接数  
        public static int connections = 0;
        public Socket client;
        int i;
        private readonly IClientService _clientService;
        public ClientThread(Socket clientsocket) {
            //service对象接管对消息的控制  
            this.client = clientsocket;
            _clientService = new HttpClientService();
        }
        public void ClientService() {
            String data = null;
            byte[] bytes = new byte[44];
            if (client != null) {
                connections++;
            }
            Console.WriteLine("新客户连接建立:{0} 个连接数", connections);
            while ((i = client.Receive(bytes)) != 0) {

                LogDTO logDto = _clientService.CoventToLog(bytes);
                _clientService.LogAddService("http://localhost/test/AddLog", logDto);

                foreach (byte b in bytes) {
                    Console.Write("{0:X2} ", b);
                }
                Console.Write("\n");
                // 处理客户端发来的消息，这里是转化为大写字母  
                //data = data.ToUpper();
                //byte[] msg = System.Text.Encoding.ASCII.GetBytes(data);

                // 发送一条应答消息  
                //service.Send(msg);
                //Console.WriteLine("发送的数据: {0}", data);
            }
            //关闭套接字  
            client.Close();
            connections--;
            Console.WriteLine("客户关闭连接:{0} 个连接数", connections);
        }
    }
}
